const app = getApp()
const car = require("../../api/car.js")
const url = require("../../utils/request.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbarData: {
      showCapsule: true, //是否显示左上角图标
      title: '购物车', //导航栏 中间的标题
      isHome: true
    },
    baseURL: url.baseURL + "cake/",
    cars: [],
    total: 0,
    page: 1,
    selectIds: [],
    height: app.globalData.height * 2 + 20 ,   
    selectTotal: 0,
    priceTotal: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    car.list(1).then(res => {
      this.setData({
        total: res.data.total,
        cars: res.data.data
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onLoad()
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      selectIds: [],
      selectTotal: 0,
      priceTotal: 0,
      cars: this.data.cars.map(item => {
        item.checked = false
      })
    })
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
   
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showLoading({
      title: '玩命加载中',
    })
    car.list(1).then(res => {
      this.setData({
        total: res.data.total,
        cars: res.data.data,
        page: 1,
        priceTotal: 0,
        selectIds: [],
        selectTotal: 0,
      })
      // 隐藏加载框
      wx.hideLoading();
      // 隐藏导航栏加载框
      wx.hideNavigationBarLoading();
      // 停止下拉动作
      wx.stopPullDownRefresh();
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let i = this.data.cars
    if(i.length == this.data.total){
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none',
        duration: 2000
      })
      return
    }else{
      wx.showLoading({
        title: '玩命加载中',
      })
    }
    this.setData({page:this.data.page+1})
    car.list(this.data.page).then((res)=>{
      // console.log(res.data);
      i.push(...res.data.data)
      // 隐藏加载框
      wx.hideLoading();
      // 隐藏导航栏加载框
      wx.hideNavigationBarLoading();
      // 停止下拉动作
      wx.stopPullDownRefresh();
      this.setData({
        cars: i
      })
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  checkboxChange: function(e){
    var ids = e.detail.value.map(item=>{
      return Number(item)
    })
    this.setData({
      selectIds: ids,
      selectTotal: e.detail.value.length,
      cars: this.data.cars.map(item => {
        if(ids.includes(item.id)){
          item.checked = true
        }else{
          item.checked = false
        }
        return item
      })
    })
    // this.setData({priceTotal: 0})
    var price = 0
    this.data.selectIds.map(item => {
      this.data.cars.map(i=>{
        if(item == i.id){
          price +=  Number(i.price.replace("￥", ""))
        }
      })
    })
    this.setData({priceTotal: price})
  },
  drop: function(){
    var ids = this.data.selectIds
    if (ids.length == 0){
      wx.showToast({
        title: '至少选择一条数据',
        icon: 'none',
        duration: 2000
      })
      return
    }

    car.drop(ids).then((res)=>{
      wx.showToast({
        title: '移除成功',
        icon: 'success',
        duration: 2000
      })
      this.setData({
        selectTotal: 0,
        selectIds:[],
        priceTotal: 0,
      })
      this.setData({cars:[]})
      for (let j = 1; j <= this.data.page;  j++) {
        car.list(j).then((res)=>{
          var i = this.data.cars
          i.push(...res.data.data)
          this.setData({
            cars: i,
            total: res.data.total
          })
        })
      }
    })
  },
  payfor: function(){
    var ids = this.data.selectIds
    var priceTotal = this.data.priceTotal
    var selectTotal = this.data.selectTotal
    if (ids.length == 0){
      wx.showToast({
        title: '至少选择一条数据',
        icon: 'none',
        duration: 2000
      })
      return
    }
    wx.navigateTo({
      url: '../payfor/index',
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('payIds', {ids, priceTotal, selectTotal})
      }
    })
  }
})